package org.smileface.designpattern.prototype;

import cn.hutool.core.collection.CollUtil;
import org.springframework.beans.BeanUtils;


/**
 * @description:
 * @author: chujian
 * @since: 2022-02-19 16:38
 **/
public class PrototypeMain {
    public static void main(String[] args) {

//        testCloneStupid();
        testBeanUtilsClone();
//        testJdkClone();
//        testDeepClone();
//        testDeepCloneByJson();

    }

    public static void testCloneStupid() {
        PrototypePerson person = new PrototypePerson()
                .setId("10001")
                .setAge(18)
                .setName("初见")
                .setHobbies(CollUtil.newArrayList("吃奶", "睡觉"));

        // 手动复制属性
        PrototypePerson clonePerson = new PrototypePerson();
        clonePerson.setName(person.getName());
        clonePerson.setAge(person.getAge());
        clonePerson.setId(person.getId());
        clonePerson.setHobbies(person.getHobbies());

        clonePerson.getHobbies().add("打豆豆");
        System.out.println("person:" + person.toString());
        System.out.println("clonePerson:" + clonePerson.toString());
    }

    public static void testBeanUtilsClone() {
        PrototypePerson person = new PrototypePerson()
                .setId("10001")
                .setAge(18)
                .setName("初见")
                .setHobbies(CollUtil.newArrayList("吃奶", "睡觉"));
        PrototypePerson clonePerson = PrototypeUtils.clone(person);

        clonePerson.getHobbies().add("打豆豆");
        System.out.println("person:" + person.toString());
        System.out.println("clonePerson:" + clonePerson.toString());
    }


    public static void testJdkClone() {
        PrototypePerson person = new PrototypePerson()
                .setId("10001")
                .setAge(18)
                .setName("初见")
                .setHobbies(CollUtil.newArrayList("吃奶", "睡觉"));

        // jdk自带的克隆
        PrototypePerson clonePerson = person.clone();

        clonePerson.getHobbies().add("打豆豆");
        System.out.println("person:" + person.toString());
        System.out.println("clonePerson:" + clonePerson.toString());
    }

    public static void testDeepClone() {
        PrototypePerson person = new PrototypePerson()
                .setId("10001")
                .setAge(18)
                .setName("初见")
                .setHobbies(CollUtil.newArrayList("吃奶", "睡觉"));
        PrototypePerson clonePerson = PrototypeUtils.deepClone(person);

        clonePerson.getHobbies().add("打豆豆");
        System.out.println("person:" + person.toString());
        System.out.println("clonePerson:" + clonePerson.toString());

    }

    public static void testDeepCloneByJson() {
        PrototypePerson person = new PrototypePerson()
                .setId("10001")
                .setAge(18)
                .setName("初见")
                .setHobbies(CollUtil.newArrayList("吃奶", "睡觉"));
        PrototypePerson clonePerson = PrototypeUtils.deepCloneByJson(person);

        clonePerson.getHobbies().add("打豆豆");
        System.out.println("person:" + person.toString());
        System.out.println("clonePerson:" + clonePerson.toString());

    }


}
